Page({
  data: {},
  onLoad({ id }) {
    if (!id) return
    this.getVisitDetail(id)
  },
  async getVisitDetail(id) {
    const { code, data: visitDetail } = await wx.http.get(`/visitor/${id}`)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      ...visitDetail,
      // validTime: 10, // 自己造一个倒计时10s
    })
  },
  onEnd() {
    this.setData({
      validTime: 0,
    })
  },
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?id=' + this.data.id,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  async onSaveLocal() {
    // 1. 根据url地址把图片下载到本地的沙盒中
    const res = await wx.getImageInfo({
      src: this.data.url,
    })
    // console.log(res)
    // 2. 把下载到沙盒中的图片文件写入到手机相册中
    await wx.saveImageToPhotosAlbum({
      filePath: res.path,
    })
    wx.utils.toast('保存到本地成功~')
  },
})
